<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Repositories\Contracts\CategoryRepositoryInterface;
use Illuminate\Http\Request;

class CategoryController extends Controller
{

  /**
   * @var \App\Repositories\CategoryRepository
   */
  private $categoryRepository;

  public function __construct(CategoryRepositoryInterface $categoryRepository)
  {
    $this->categoryRepository=$categoryRepository;
  }

  public function lists($id = null)
  {
    $result = $this->categoryRepository->find($id);

    return response()->json(['success' => true, 'data' => $result]);
  }

  public function save(Request $request)
  {
    $category = $this->categoryRepository->saveModel($request->all());
    return response()->json(['success' => true, 'data' => $category]);
  }


  public function remove($id = null)
  {
    $id = $id ? intval($id) : 0;
    if ($id < 1) {
      return response()->json(['success' => false]);
    }
    $result =  $this->categoryRepository->delete($id);
    return response()->json(['success' => ($result > 0)]);
  }

  public function show($id = null)
  {
    $id = $id ? intval($id) : 0;
    if ($id < 1) {
      return response()->json(['success' => false]);
    }
    $result =  $this->categoryRepository->find($id);
    return response()->json(['success' => true, 'data' => $result]);
  }
}
